<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./vue.js"></script>
  <script src="https://cdn.bootcss.com/Swiper/4.5.1/js/swiper.min.js"></script>
  <link href="https://cdn.bootcss.com/Swiper/4.5.1/css/swiper.min.css" rel="stylesheet">
  <style>
  .swiper-container {
    width: 800px;
    height: 500px;
  }
  </style>
</head>
<body>
  <div id="app">
    <div class="swiper-container">
      <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="item in banner" :key="item.id">{{ item.title }}</div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>
  </div>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        banner: []
      },
      created () {
        // 在这里发送ajax请求，拿到swiper的数据渲染轮播图
        // 用定时器模拟一下
        // 有可能在拿到数据以后DOM还没有更新
        setTimeout(() => {
          this.banner = [
            { id: 1, title: 'slider1' },
            { id: 2, title: 'slider2' },
            { id: 3, title: 'slider3' }
          ]
          // 应该banner的修改相应到真实DOM之后才能调用swiper
          Vue.nextTick().then(() => {
            this.initSwiper()
          })
        }, 100)
      },
      methods: {
        initSwiper () {
          var mySwiper = new Swiper ('.swiper-container', {
          loop: true, // 循环模式选项
          
          // 如果需要分页器
          pagination: {
            el: '.swiper-pagination',
          },
          
          // 如果需要前进后退按钮
          navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
          },
          
          // 如果需要滚动条
          scrollbar: {
            el: '.swiper-scrollbar',
          },
        })    
        }
      }
    })
  </script>
</body>
</html>
